<?php
class UserMahasiswa extends Model {
	function UserMahasiswa(){
		parent::Model();
    }
    function getNama($uid){
    	$result= array();
        $query = "SELECT * FROM user WHERE id='$uid' LIMIT 1";
    	$query = $this->db->query($query);
    	
    	if($query->result()){
    		foreach ($query->result() as $row){
  				$mid= $row->mahasiswaId;
			}
	    	$query = "SELECT * FROM mahasiswa WHERE id='$mid' LIMIT 1";
	    	$query = $this->db->query($query);
	    	
	    	if($query->result()){
	    		foreach ($query->result() as $row){
	  				$result ['nama']= $row->nama;
	  				$result ['nim']= $row->nim;
	  				$result ['email']= $row->email;
				}
				$result['ok'] = TRUE;
	    	}else{
	    		$result['ok'] = FALSE;
	    	}
    	}else{
    		$result['ok'] = FALSE;
    	}
        return json_encode($result);
    }
    
	function getNim($uid){
    	$result= array();
        $query = "SELECT * FROM user WHERE id='$uid' LIMIT 1";
    	$query = $this->db->query($query);
    	
    	if($query->result()){
    		foreach ($query->result() as $row){
  				$mid= $row->mahasiswaId;
			}
	    	$query = "SELECT * FROM mahasiswa WHERE id='$mid' LIMIT 1";
	    	$query = $this->db->query($query);
	    	
	    	if($query->result()){
	    		foreach ($query->result() as $row){
	  				$result ['nim']= $row->nim;
				}
				$result['ok'] = TRUE;
	    	}else{
	    		$result['ok'] = FALSE;
	    	}
    	}else{
    		$result['ok'] = FALSE;
    	}
        return json_encode($result);
    }
    
    
    function login($username,$password){
    	$password = md5($password);
    	$result['error'] = '';
        $query = "SELECT * FROM user WHERE username='$username' LIMIT 1";
    	$query = $this->db->query($query);
    	if($query->num_rows() < 1){
    		$result['error'] = 'Kesalahan pada Username';
    		$query = "SELECT * FROM mahasiswa WHERE nim='$username' LIMIT 1";
    		$query = $this->db->query($query);
    		if($query->num_rows() < 1){
    			$result['error'] = 'Username/NIM belum terdaftar';
    		}else{
				foreach ($query->result() as $row){
			      $mId  = $row->id;
			   	}
				$query = "SELECT * FROM user WHERE mahasiswaId='$mId' AND password='$password' LIMIT 1";
    			$query = $this->db->query($query);
    			if($query->num_rows() < 1){
    				$result['error'] = 'Password tidak sesuai dengan NIM';
    			}else{
    				$result['error'] = '';
	    			foreach ($query->result() as $row){
				      $result['id']  = $row->id;
				   	}
    			}
    		}
    	}else{
    		$query = "SELECT * FROM user WHERE username='$username' AND password='$password' LIMIT 1";
    		$query = $this->db->query($query);
    		if($query->num_rows() < 1){
    			$result['error'] = 'Password tidak sesuai dengan Username';
    		}else{
    			$result['error'] = '';
    			foreach ($query->result() as $row){
					$result['id']  = $row->id;
				}
    		}
    	}
    	return $result;
    }
}